<?php
/*
*  -------------------------------------------------
*   @file		: .php
*   @link		:CPC  www.chinapet.com
*   @copyright	: 2002-2012 Xingmo Inc
*   @author		: Laipiyang <462166282@qq.com>
*   @blog_url   : http://www.icuir.com/
*   @blog_url   : http://blog.sina.com.cn/xmphp
*   @date		:
*   @update		:
*  -------------------------------------------------
*/
require "./include/init.inc.php";
checklogin();
$filename=basename(__file__);
//checkpower();
$act=isset ( $_GET['act'] ) ? trim ( $_GET['act'] ) : '' ;
if($act=='add')
{
	 $smarty->assign('web',array('title'=>'私信添加','act'=>'insert','btn'=>'私信添加完成'));
	 $smarty->assign('user_list',$a=$db->fetch_all("SELECT `id`,`nick` FROM `info_link_user` WHERE 1"));
	 $smarty->display(ADMIN_TPL.'private_letter_info.dwt');

}
else if ($act=='insert')
{
  $send_user_id=50887;  //设置系统管理员帐号发送给每一个用户进行私信设置
  $get_user_ids=isset($_POST['get_user_ids']) ? $_POST['get_user_ids'] : array();
  if(empty($get_user_ids))
  {
	 alert('对不起,私信接收者必选');
	 exit;
  }
  $content=isset ( $_POST['content'] ) ? trim ( $_POST['content'] ) : '' ;
  if(empty($content) || !isset($content))
  {
	 alert('私信内容必填!');
	 exit;
  }
  $state=isset ( $_POST['state'] ) ? intval ( $_POST['state'] ) : 0;
  if($state)
  {
	  $state=0;   //设置城1是为了给用户进行相关的未读私信,默认添加以未读。
  }

  $dateline=time();
  $sql="INSERT INTO `info_q_message` (`send_user_id`,`get_user_id`,`content`,`state`,`dateline`) VALUES ";
  foreach ($get_user_ids as $key => $val )
  {
		if($val)
		{
			 $sql.="('{$send_user_id}','{$val}','{$content}','{$state}','{$dateline}') ,";
		}
  }
   $sql=substr($sql,0,-1);
   $res=$db->query($sql);
   if($res)
   {
	  alert('私信添加成功',$filename);
	  exit;
   }
   else
   {
	  alert('私信添加失败');
	  exit;
   }

}
else if ($act=='edit')
{
	echo 5;

//	$id=isset ( $_GET['id'] ) ? intval ( $_GET['id'] ) : 0 ;
//	if($id<1)
//	{
//		alert('操作非法');
//		exit;
//	}
//	$sql="SELECT * FROM `info_q_message` WHERE `id`='{$id}'";
//	$query=$db->fetch_one($sql);
//	if(!$query)
//	{
//		alert('记录不存在');
//		exit;
//	}
//	$smarty->assign('row',$query);
//	$smarty->assign('user_list',$a=$db->fetch_all("SELECT `id`,`nick` FROM `info_link_user` WHERE 1"));
//	$smarty->assign('web',array('title'=>'私信修改页面','act'=>'update','btn'=>'私信修改完成'));
//	$smarty->display(ADMIN_TPL.'private_letter_info.dwt');

}
else if ($act=='update')
{
//     $get_user_id=isset ( $_POST['get_user_ids'] ) ? intval ( $_POST['get_user_ids'] ) : 0;
//	  $content=isset ( $_POST['content'] ) ? trim ( $_POST['content'] ) : '' ;


}
else if ($act=='del')
{
	$id=isset ( $_GET['id'] ) ? intval ( $_GET['id'] ) : 0 ;
	if($id<1)
	{
		alert('操作非法');
		exit;
	}
	$sql="DELETE FROM `info_q_message` WHERE `id`='{$id}' ";
	$res=$db->query($sql);
	if($res)
	{
		alert('私信删除成功',$filename);
		exit;
	}
	else
	{
		alert('私信删除失败');
		exit;
	}

}
else
{
	$sql="SELECT * FROM `info_q_message` ORDER BY `dateline` DESC";
    $smarty->assign('pl',$pl=licuicui_page($db->num_rows($db->query($sql)),$filename));
	$smarty->assign('web',array('title'=>'私信列表页面'));
	$smarty->assign('mes_list',$db->fetch_all("SELECT q.*,u.`nick` FROM `info_q_message` AS q LEFT JOIN `info_link_user` AS u ON q.`get_user_id`=u.`id` LIMIT {$pl['start']},{$pl['pagesize']}"));
	 $smarty->display(ADMIN_TPL.'private_letter_list.dwt');

}
?>